<?php

require_once "application.php";

class UzytkownikController extends application {

    public function init() {
        parent::init();
    }

    public function indexAction() {
        $auth = Zend_Auth::getInstance();
        if ($auth->hasIdentity()) {
            $this->view->identity = $auth->getIdentity();
        }
    }
    
    public function logopedziAction() {
        
        Zend_Loader::loadClass('Uzytkownik');
        $users = new Uzytkownik();
        $userlist = $users->fetchAll('rola="admin" OR rola="operator"');
        $this->view->lista = $userlist;
    }
    
    public function edycjaAction() {
        Zend_Loader::loadClass('Uzytkownik');
        $users = new Uzytkownik();
        $id = $this->_request->getParam('id');
        if($id>0) $user = $users->fetchRow('id='.$id);
        $form = new Application_Form_Logopeda();
        if($_POST) {
            if($form->valid($_POST)) {
                if($id==0) $user = $users->createRow ();
                $user->imie = $this->_request->getParam('imie');
                $user->nazwisko = $this->_request->getParam('nazwisko');
                if(strlen($this->_request->getParam('haslo'))>3) $user->haslo = md5($this->_request->getParam('haslo'));
                $user->email = $this->_request->getParam('email');
                $user->rola = $this->_request->getParam('rola');
                $user->active = $this->_request->getParam('active');
                $user->save();
                if($user->active == 1) {
                    $message = "
                    Witam {$user->imie} {$user->nazwisko}.
                    Założono lub zmodyfikowano konto w systemie backoffice Elogo.
                    Login to: {$user->email}";
                    if(strlen($this->_request->getParam('haslo'))>3) {
                    $message .="
                    Hasło tymczasowe to: ".$this->_request->getParam('haslo')."
                    Adres systemu: http://backoffice.elogo.edu.pl
                    Prosimy o zalogowanie się i zmianę hasła.
                    ";
                    }
                    $subject = "Modyfikacja konta backoffice ELOGO.";
                    mail($user->email, $subject, $message);
                }
            }
            $form->populate($_POST);
        } else {
            if($id>0) {
                $user->haslo = '';
                $form->populate($user->toArray());
            } else {
                
            }
        }
        $this->view->form = $form;
        $this->view->id = $id;
    }

    public function profilAction() {
        $auth = Zend_Auth::getInstance();
        if ($auth->hasIdentity()) {
            $this->view->identity = $auth->getIdentity();
        }
        Zend_Loader::loadClass('Firma');
        $firma = new Firma();
        $this->view->aktywne = $firma->showActiveCompanyForUser($this->_domainid, $auth->getIdentity()->id);
        $this->view->nieaktywne = $firma->showInactvieCompanyForUser($this->_domainid, $auth->getIdentity()->id);
    }

    public function edycjaUzytkownikaAction() {
        $auth = Zend_Auth::getInstance();
        if ($auth->hasIdentity()) {
            $this->view->identity = $auth->getIdentity();
        }
        Zend_Loader::loadClass('Uzytkownik');
        $uzytkownik = new Uzytkownik();
        $user = $uzytkownik->find($auth->getIdentity()->id)->current();
        $language = 'pl';
        $translator = new Zend_Translate(
                        'array',
                        '../application/resources/languages',
                        $language
        );
        Zend_Validate_Abstract::setDefaultTranslator($translator);
        $form = new Application_Form_Uzytkownik();
        $form->setAction('/uzytkownik/edycja-uzytkownika')
                ->setMethod('post');
        // usuwamy pole z hasłem
        $form->removeElement('password');
        $form->removeElement('password_confirm');
        if ($this->getRequest()->isPost()) {
            if ($form->isValid($_POST)) {
                $id = $form->getValue('id');
                $email = $form->getValue('email');
                $imie = $form->getValue('imie');
                $nazwisko = $form->getValue('nazwisko');
                $firma = $form->getValue('firma');
                $result = $uzytkownik->edycjaUzytkownika($id, $email, $imie, $nazwisko, $firma);
                if ($result) {
                    return $this->_redirect('/uzytkownik/edycja-uzytkownika-finish');
                }
            }
            if ($form->isErrors()) {
                $form->populate($_POST);
            }
        } else {
            $form->populate($user->toArray());
        }
        $this->view->form = $form;
    }
    
    public function uczniowieAction() {
        Zend_Loader::loadClass('Uzytkownik');
        $users = new Uzytkownik();
        $lista = $users->fetchAll("rola='user'");
        $this->view->lista = $lista;
        
    }

    public function resetHaslaAction() {
        $auth = Zend_Auth::getInstance();
        if ($auth->hasIdentity()) {
            $this->view->identity = $auth->getIdentity();
        }
        $id = $this->_request->getParam('id');
        Zend_Loader::loadClass('Uzytkownik');
        $uzytkownik = new Uzytkownik();
        $user = $uzytkownik->find($auth->getIdentity()->id)->current();
        $language = 'pl';
        $translator = new Zend_Translate(
                        'array',
                        '../application/resources/languages',
                        $language
        );
        Zend_Validate_Abstract::setDefaultTranslator($translator);
        $form = new Application_Form_Uzytkownik();
        $form->setAction('/uzytkownik/reset-hasla')->setMethod('post');
        $form->removeElement('email');
        $form->removeElement('imie');
        $form->removeElement('nazwisko');
        $form->removeElement('firma');
        if ($this->getRequest()->isPost()) {
            if ($form->isValid($_POST)) {
                $id = $form->getValue('id');
                $haslo = md5($form->getValue('haslo'));
                $result = $uzytkownik->resetHasla($id, $haslo);
                if ($result) {
                    return $this->_redirect('/uzytkownik/profil');
                }
            }
            if ($form->isErrors()) {
                $form->populate($_POST);
            }
        } else {
            // wypełniemy tylko ukryte pole id w formularzu
            $form->getElement('id')->setValue($user->id);
        }
        $this->view->form = $form;
    }

    public function usunFirmaAction() {
        Zend_Loader::loadClass('Firma');
        $firma = new Firma();
        $id = $this->_request->getParam("id");
        $where = $firma->getAdapter()->quoteInto('id=?', $id);
        $firma->delete($where);
        $this->_redirect('/uzytkownik/usun-firma-finish/');
    }

    public function usunFirmaFinishAction() {

    }

    public function edycjaUzytkownikaFinishAction() {

    }

    public function edycjaFirmyDaneAction() {
        $auth = Zend_Auth::getInstance();
        if ($auth->hasIdentity()) {
            $this->view->identity = $auth->getIdentity();
        }
        $uid = $auth->getIdentity()->id;
        Zend_Loader::loadClass('Firma');
        $firma = new Firma();
        $id = $this->_request->getParam("id");
        $firma_dane = $firma->find($id)->current();
        $language = 'pl';
        $translator = new Zend_Translate(
                        'array',
                        '../application/resources/languages',
                        $language
        );
        Zend_Validate_Abstract::setDefaultTranslator($translator);
        $form = new Application_Form_Firma();
        $form->setAction('/uzytkownik/edycja-firmy-dane')
                ->setMethod('post');
        $form->removeElement('captcha');
        $form->getElement('dodaj')->setLabel("zapisz");
        if ($this->getRequest()->isPost()) {
            if ($form->isValid($_POST)) {
                $id = $form->getValue('id');
                $nazwa = $form->getValue('nazwa');
                $miasto = $form->getValue('miasto');
                $postcode = $form->getValue('postcode');
                $ulica = $form->getValue('ulica');
                $telefon = $form->getValue('telefon');
                $email = $form->getValue('email');
                $www = $form->getValue('www');
                $result = $firma->edycjaFirmyDane($id, $nazwa, $miasto, $postcode, $ulica, $telefon, $email, $www, $uid);
                if ($result) {
                    return $this->_redirect('/uzytkownik/edycja-firmy-dane-finish');
                }
            }
            if ($form->isErrors()) {
                $form->populate($_POST);
            }
        } else {
            $form->populate($firma_dane->toArray());
        }
        $this->view->form = $form;
    }

    public function edycjaFirmyDaneFinishAction() {

    }

    public function edycjaFirmyLokalizacjaAction() {
        $auth = Zend_Auth::getInstance();
        if ($auth->hasIdentity()) {
            $this->view->identity = $auth->getIdentity();
        }
        $uid = $auth->getIdentity()->id;
        Zend_Loader::loadClass('Firma');
        $firma = new Firma();
        $id = $this->_request->getParam("id");
        $firma_dane = $firma->find($id)->current();
        $language = 'pl';
        $translator = new Zend_Translate(
                        'array',
                        '../application/resources/languages',
                        $language
        );
        Zend_Validate_Abstract::setDefaultTranslator($translator);
        $form = new Application_Form_Firmab();
        $form->setAction('/uzytkownik/edycja-firmy-lokalizacja')
                ->setMethod('post');
        if ($this->getRequest()->isPost()) {
            if ($form->isValid($_POST)) {
                $id = $form->getValue('id');
                $long = $form->getValue('long');
                $lat = $form->getValue('lat');
                $result = $firma->edycjaFirmyLokalizacja($id, $long, $lat, $uid);
                if ($result) {
                    return $this->_redirect('/uzytkownik/edycja-firmy-lokalizacja-finish');
                }
            }
            if ($form->isErrors()) {
                $form->populate($_POST);
            }
        } else {
            $form->populate($firma_dane->toArray());
        }
        $this->view->form = $form;
    }

    public function edycjaFirmyLokalizacjaFinishAction() {

    }

}